package cn.iocoder.yudao.module.member.controller.admin.userpackage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userpackage.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpackage.UserPackageDO;
import cn.iocoder.yudao.module.member.service.userpackage.UserPackageService;

@Tag(name = "管理后台 - 用户关联套餐")
@RestController
@RequestMapping("/member/user-package")
@Validated
public class UserPackageController {

    @Resource
    private UserPackageService userPackageService;

    @PostMapping("/create")
    @Operation(summary = "创建用户关联套餐")
    @PreAuthorize("@ss.hasPermission('member:user-package:create')")
    public CommonResult<Long> createUserPackage(@Valid @RequestBody UserPackageSaveReqVO createReqVO) {
        return success(userPackageService.createUserPackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户关联套餐")
    @PreAuthorize("@ss.hasPermission('member:user-package:update')")
    public CommonResult<Boolean> updateUserPackage(@Valid @RequestBody UserPackageSaveReqVO updateReqVO) {
        userPackageService.updateUserPackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关联套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-package:delete')")
    public CommonResult<Boolean> deleteUserPackage(@RequestParam("id") Long id) {
        userPackageService.deleteUserPackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关联套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-package:query')")
    public CommonResult<UserPackageRespVO> getUserPackage(@RequestParam("id") Long id) {
        UserPackageDO userPackage = userPackageService.getUserPackage(id);
        return success(BeanUtils.toBean(userPackage, UserPackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户关联套餐分页")
    @PreAuthorize("@ss.hasPermission('member:user-package:query')")
    public CommonResult<PageResult<UserPackageRespVO>> getUserPackagePage(@Valid UserPackagePageReqVO pageReqVO) {
        PageResult<UserPackageDO> pageResult = userPackageService.getUserPackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户关联套餐 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-package:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPackageExcel(@Valid UserPackagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPackageDO> list = userPackageService.getUserPackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户关联套餐.xls", "数据", UserPackageRespVO.class,
                        BeanUtils.toBean(list, UserPackageRespVO.class));
    }

}